tool
extends EditorPlugin

const MyInspectorPlugin = preload("res://addons/puzzle_polyhedron/MyInspectorPlugin.gd")

var my_inspector_plugin: MyInspectorPlugin
var map_data_editor: MyGridMapEditor
var map_data_editor_menu: MyGridMapEditorMenu

class MapDataEditorAdaptor:
	extends MyGridMapEditor.Adaptor
	func get_name() -> String:
		return "MapData"
	func is_valid(object: Spatial) -> bool:
		var md: MapData = object as MapData
		return md != null and md.tile_type_map != null
	func get_tile_id(object: Spatial, position: Vector3) -> String:
		var md: MapData = object as MapData
		return md.get_tile_id(position)
	func set_tile_by_id(object: Spatial, position: Vector3, tile_id: String) -> void:
		var md: MapData = object as MapData
		md.set_tile_by_id(position, tile_id)
	func refresh_tile_types(object: Spatial) -> void:
		tile_ids = [""]
		tile_names = ["Empty"]
		var md: MapData = object as MapData
		if md != null and md.tile_type_map != null:
			var ttm: TileTypeMap = md.tile_type_map
			ttm.update_tile_type_map()
			for tile_id in ttm.tile_type_map:
				tile_ids.push_back(tile_id)
				tile_names.push_back("%s (%s)" % [ttm.tile_type_map[tile_id].name, tile_id])
	func lookup(object: Spatial, tile_id: String):
		var md: MapData = object as MapData
		if md != null and md.tile_type_map != null:
			return md.tile_type_map.lookup(tile_id)
		return null
	func create_instance(tile_type) -> Spatial:
		var tt: TileType = tile_type as TileType
		if tt != null:
			return tt.create_instance()
		return null
	func get_used_cells(object: Spatial) -> Array:
		var md: MapData = object as MapData
		if md != null and md.tile_type_map != null:
			var ret: Array = []
			for v in md.map_data:
				if not (md.map_data[v] as String).empty():
					ret.push_back(v)
			return ret
		return []

class CuboidPolyhedronEditorAdaptor:
	extends MyGridMapEditor.Adaptor
	func get_name() -> String:
		return "CuboidPolyhedron"
	func is_valid(object: Spatial) -> bool:
		var obj: CuboidPolyhedron = object as CuboidPolyhedron
		return obj != null
	func get_tile_id(object: Spatial, position: Vector3) -> String:
		var obj: CuboidPolyhedron = object as CuboidPolyhedron
		return obj.get_tile_id(position)
	func set_tile_by_id(object: Spatial, position: Vector3, tile_id: String) -> void:
		var obj: CuboidPolyhedron = object as CuboidPolyhedron
		obj.set_tile_by_id(position, tile_id)
	func refresh_tile_types(object: Spatial) -> void:
		tile_ids = ["", "SOLID", "ANTENNA"]
		tile_names = ["EMPTY", "SOLID", "ANTENNA"]
	func lookup(object: Spatial, tile_id: String):
		var idx = CuboidPolyhedron.PolyhedronCellMap.get(tile_id)
		if idx != null:
			return [object, idx]
		return null
	func create_instance(tile_type) -> Spatial:
		if tile_type != null:
			return (tile_type[0] as CuboidPolyhedron).create_poly_instance(tile_type[1])
		return null
	func get_used_cells(object: Spatial) -> Array:
		var obj: CuboidPolyhedron = object as CuboidPolyhedron
		if obj != null:
			var ret: Array = []
			for v in obj.data:
				if obj.data[v] > 0:
					ret.push_back(v)
			return ret
		return []

func _enter_tree():
	my_inspector_plugin = MyInspectorPlugin.new()
	add_inspector_plugin(my_inspector_plugin)

	map_data_editor = preload("res://addons/puzzle_polyhedron/MyGridMapEditor.tscn").instance()
	add_control_to_container(CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT, map_data_editor)
	map_data_editor_menu = preload("res://addons/puzzle_polyhedron/MyGridMapEditorMenu.tscn").instance()
	add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, map_data_editor_menu)

	map_data_editor.undo_redo = get_undo_redo()

	map_data_editor.setup(map_data_editor_menu)
	map_data_editor_menu.setup()

	make_visible(false)

func _exit_tree():
	remove_inspector_plugin(my_inspector_plugin)
	my_inspector_plugin = null

	remove_control_from_container(CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT, map_data_editor)
	map_data_editor.queue_free()
	remove_control_from_container(CONTAINER_SPATIAL_EDITOR_MENU, map_data_editor_menu)
	map_data_editor_menu.queue_free()

func handles(object):
	return object is MapData or object is CuboidPolyhedron

func edit(object):
	if object is MapData:
		map_data_editor.adaptor = MapDataEditorAdaptor.new()
	elif object is CuboidPolyhedron:
		map_data_editor.adaptor = CuboidPolyhedronEditorAdaptor.new()
	else:
		printerr("ERROR: Class '%s' is unrecognized for custom editor" % object.get_class())
		return

	map_data_editor.edit(object)

func clear():
	map_data_editor.edit(null)
	map_data_editor.set_process(false)
	map_data_editor_menu.set_process(false)

func forward_spatial_gui_input(camera, event):
	return map_data_editor.forward_spatial_gui_input(camera, event)

func make_visible(visible):
	if visible:
		map_data_editor.show()
		map_data_editor_menu.show()
		map_data_editor.set_process(true)
		map_data_editor_menu.set_process(true)
	else:
		map_data_editor.hide()
		map_data_editor_menu.hide()
		map_data_editor.edit(null)
		map_data_editor.set_process(false)
		map_data_editor_menu.set_process(false)
